<?php
/**
 * RUBIK IT Solution & Development ltd.
 *
 * PROJECT		: Rubik Ultimate
 * @version 	: 1.1
 * COPYRIGHT	: 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * @author			hoadn <hoadn@rubik.com.vn>
 * @package		 	core.html.filedInfo
 *
 * Created on	:Sep 19, 2008
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls('html.fieldInfo.BasicFileHtmlFieldInfo');
RCoreImporter::cls("helper.RFileFolderHelper" );

class UploadMultiFileHtmlFieldInfo extends BasicFileHtmlFieldInfo {
	
	var $k = ".gif,.jpg,.png,.txt,.pdf,.doc,.zip,.rar,.tar,.gz,.txt,.xml";
	var $_thumbSize;
	var $_largeSize;
	
	/**
	 * Constructor
	 *
	 * @param string $basicName
	 */
	function __construct($basicName, $linkRule = null) {
		parent::__construct($basicName, $linkRule);
		$this->_thumbSize = array('width'=>100, 'height'=>50);
		$this->_largeSize = array('width'=>null, 'height'=>null);
	}
	
	/**
	 * Set the thumbnail size
	 *
	 * @param int $width
	 * @param int $height
	 */
	function setThumbSize($width, $height) {
		 $this->_thumbSize = array('width'=>$width, 'height'=>$height);
	}

	/**
	 * Set the large size
	 *
	 * @param int $width
	 * @param int $height
	 */
	function setLargeSize($width, $height) {
		 $this->_largeSize = array('width'=>$width, 'height'=>$height);
	}
	
	/**
	 * Function for geting the html for inputing data
	 *
	 * @abstract 
	 * @return string
	 */
	function getHtmlInputData($dataType = '', $extAttribute = '') {
		SysResponse::addJavascriptFile(SYS_ROOT_URL . "libraries/js/multifile_upload.js");
		$script = "
		<script type=\"text/javascript\">
			var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 10, '{$this->k}' );
			multi_selector.addElement( document.getElementById( '".$this->_basicName."_id' ) );
		</script>";
		
		$html = '
			<div>
   				<input id="'.$this->_basicName.'_id" type="file" name="'.$this->_basicName.'" />
   			</div>
   			<div id="div_attach">
	   			Valid file extensions: '.$this->k.'
				<div id="status"></div>
				<div id="files_list"></div>
			</div>'.$script;
		return $html;
	}
	
	/**	
	 * Set value for this
	 * @param  string $pathToUpload
	 * @return msg 
	 */
	function uploadValue($pathToUpload) {
		$result = array();
		$html = '';
		$aFile = $_FILES;
		$msg = "";
		RCoreImporter::cls("helper.RFileFolderHelper" );
		for($i = 0;;$i++){
			$child = $aFile['file_'.$i];
			if(!empty($child['name'])){
				$file_name = $child['name'];
				$temp_file = $child['tmp_name'];
				$folder = SYS_ROOT_PATH.$pathToUpload;
				$sUploadValue = RFileFolderHelper::uploadFile($temp_file, $file_name, $folder, explode(',',$this->k) );
				if($sUploadValue === false){
					echo "Upload file ".$child['name']."error";
				}else{
					$html .= '<a href="'.SYS_ROOT_URL.$pathToUpload.$sUploadValue.'">'.$child['name'].'</a><br/>'; 						
					$result[] = $pathToUpload.$sUploadValue;
				}
			}else{
				break;
			}
		}
		return $result;
		return $html;
	}
	
}
?>
